package net.flyinggroup.kafka.fromroute.Deprecated;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.binding.BinderAwareChannelResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.function.Consumer;
import java.util.function.Function;

@SpringBootApplication
@Controller
public class DeprecatedSourceWithDynamicDestination {

    @Autowired
    private BinderAwareChannelResolver resolver;

    public static void main(String[] args) {
        SpringApplication.run(DeprecatedSourceWithDynamicDestination.class,
                "--spring.cloud.function.definition=even");
    }

    @Bean
    public Consumer<String> even() {
        return value -> System.out.println("Even:" + value);
    }

    @Bean
    public Function<String, String> upperCase() {
        return String::toUpperCase;
    }


    @RequestMapping(value = "/{target}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public void send(@RequestBody String body, @PathVariable("target") String target) {
        resolver.resolveDestination(target).send(new GenericMessage<>(body));
    }
}
